//
//  YMMyModel.m
//  GuoAo
//
//  Created by y s on 2023/3/9.
//

#import "YMMyModel.h"

#define kSave_user_infor_data @"save_user_infor_data_key"

@implementation YMMyModel

+ (instancetype)shared{
    static dispatch_once_t onceToken;
    static YMMyModel *sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[YMMyModel alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init{
    self = [super init];
    if (self){
        [self initData];
    }
    return self;
}
- (void)initData{
    self.token = @"";
    self.userId = @"";
    self.tuoTuToken = @"";
    self.deviceId = @"";
}
#pragma mark -- public methods
- (void)launch{
    if ([SLocalUtil hasKey:kSave_user_infor_data]){
        NSDictionary *dict = [SLocalUtil dictForKey:kSave_user_infor_data];
        [self updateData:dict];
    }
}

- (void)updateData:(NSDictionary *)dict{
    if (dict != nil && [dict isKindOfClass:[NSDictionary class]]){
        self.token = [SDataUtil stringOf:dict[@"token"]];
        self.tuoTuToken = [SDataUtil stringOf:dict[@"tuoTuToken"]];
        self.userId = [SDataUtil stringOf:dict[@"tuoTuUserId"]];
        self.userName = [SDataUtil stringOf:dict[@"tuoTuUserName"]];
        self.userPhone = [SDataUtil stringOf:dict[@"tuoTuUserPhone"]];
    }
}

+ (BOOL)isLogin{
    if ([YMMyModel shared].token != nil &&
        [YMMyModel shared].token.length > 0){
        return YES;
    }
    return NO;
}
/** 更新登录数据 */
- (void)loginSuccess:(NSDictionary *)dict{
    if (dict != nil && [dict isKindOfClass:[NSDictionary class]]){
        [self updateData:dict];
        [SLocalUtil setDict:dict forKey:kSave_user_infor_data];
        [[NSNotificationCenter defaultCenter] postNotificationName:LoginedNoti_key object:nil];
    }
}
- (void)logout{
    [self initData];
    if ([SLocalUtil hasKey:kSave_user_infor_data]){
        [SLocalUtil removeKey:kSave_user_infor_data];
        [[NSNotificationCenter defaultCenter] postNotificationName:LogoutNoti_key object:nil];
    }
}


@end
